package com.samuel.basics.engine;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;

/**
 * @author kaiji
 */
public class InsistAutoGeneratorEngine extends AutoGenerator {

    /**
     * 数据源配置
     */
    private DataSourceConfig dataSource;
    /**
     * 数据库表配置
     */
    private StrategyConfig strategy;
    /**
     * 包 相关配置
     */
    private PackageConfig packageInfo;
    /**
     * 模板 相关配置
     */
    private TemplateConfig template;
    /**
     * 全局 相关配置
     */
    private GlobalConfig globalConfig;
    /**
     * 模板引擎
     */
    private InsistTemplateEngine templateEngine;

    @Override
    public void execute() {
        // 初始化配置
        if (null == config) {
            config = new ConfigBuilder(packageInfo, dataSource, strategy, template, globalConfig);
            if (null != injectionConfig) {
                injectionConfig.setConfig(config);
            }
        }
        // 模板引擎初始化执行文件输出
        templateEngine.init(this.pretreatmentConfigBuilder(config))
                .mkdirs()
                .createMenu()
                .batchOutputs()
                .open();
    }

    @Override
    public DataSourceConfig getDataSource() {
        return dataSource;
    }

    @Override
    public AutoGenerator setDataSource(DataSourceConfig dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    @Override
    public StrategyConfig getStrategy() {
        return strategy;
    }

    @Override
    public AutoGenerator setStrategy(StrategyConfig strategy) {
        this.strategy = strategy;
        return this;
    }

    @Override
    public PackageConfig getPackageInfo() {
        return packageInfo;
    }

    @Override
    public AutoGenerator setPackageInfo(PackageConfig packageInfo) {
        this.packageInfo = packageInfo;
        return this;
    }

    @Override
    public TemplateConfig getTemplate() {
        return template;
    }

    @Override
    public AutoGenerator setTemplate(TemplateConfig template) {
        this.template = template;
        return this;
    }

    @Override
    public GlobalConfig getGlobalConfig() {
        return globalConfig;
    }

    @Override
    public AutoGenerator setGlobalConfig(GlobalConfig globalConfig) {
        this.globalConfig = globalConfig;
        return this;
    }

    @Override
    public InsistTemplateEngine getTemplateEngine() {
        return templateEngine;
    }

    public void setTemplateEngine(InsistTemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }
}
